package com.wanma.recuit.bosscontroller;

import com.github.pagehelper.PageInfo;
import com.wanma.recuit.pojo.*;
import com.wanma.recuit.service.ManageResumeService;
import com.wanma.recuit.util.TimestampEditor;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Controller
public class ManageResumeController {
//    //日期的格式化
//    @InitBinder
//    public void init(WebDataBinder binder){
//        DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
//        binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat,true));
//        binder.registerCustomEditor(Timestamp.class, new TimestampEditor("yyyy-mm-dd HH:mm:ss"));
//    }


    @Resource
    private ManageResumeService manageResumeService;
    //到管理简历的界面 而且是申请简历的页面
    @GetMapping("/bossmanage/manageresume")
    public ModelAndView manageresume(HttpServletRequest request, ModelAndView modelAndView){
        HttpSession session = request.getSession();
        CompanyUser companyUser = (CompanyUser) session.getAttribute("companyUser");
       //把待处理的简历个数查出来
        Integer resumeCount = manageResumeService.findPendingResumeNumber(companyUser,34);
        //查出字典表的学历，工作经历
        List<Dictionary>  edu = manageResumeService.findEduExperience1();
        List<Dictionary>  work = manageResumeService.findWorkExperience1();
        //查出发布的职位
        List<Job> jobs = manageResumeService.findJob1(companyUser);
        modelAndView.addObject("resumeCount",resumeCount);
        modelAndView.addObject("edu",edu);
        modelAndView.addObject("work",work);
        modelAndView.addObject("jobs",jobs);
        return modelAndView;
    }
    //申请简历页面的显示 单独的一个页面
    @PostMapping("/bossmanage/waitmanageresume")
    @ModelAttribute("pageInfo")
    public PageInfo<Apply> waitManageResume(Integer jobId,Integer workId,Integer eduId,Integer pageNum,Integer num,HttpServletRequest request){
        HttpSession session = request.getSession();
        CompanyUser companyUser = (CompanyUser) session.getAttribute("companyUser");

        //把待处理的简历全部查出来
        PageInfo<Apply> pageInfo = manageResumeService.findPendingResume(jobId,workId,eduId,pageNum,num,companyUser);
//
//        System.out.println(pageInfo.getList().get(0).getUser().getName());
        return pageInfo;

    }
    //预览简历  查出简历的所有信息
    @GetMapping("/bossmanage/resumeView")
    @ModelAttribute("resume")
    public Resume resumePreview(Integer id){
        Resume resume = manageResumeService.findResume(id);
//        System.out.println(resume.getExpectationList().get(0).getCity1());
        return resume;
    }
    //发出简历邀请 并修改申请表简历邀请的状态
    @PostMapping("/bossmanage/updateresumestatus")
    public Integer updateResumeStatus(Apply apply,Integer userId,Integer jobId,String interviewTime){

        Date date = null;
        //获得SimpleDateFormat类，我们转换为yyyy-MM-dd的时间格式
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            //使用SimpleDateFormat的parse()方法生成Date
            date = sf.parse(interviewTime);
            //打印Date
        } catch ( ParseException e) {
            e.printStackTrace();
        }
        apply.setInterviewTime(date);
        System.out.println(apply.getInterviewTime());
        User user = new User();
        user.setId(userId);
        apply.setUser(user);
        Job job = new Job();
        job.setId(jobId);
        apply.setJob(job);
        manageResumeService.sendInterview(apply);
        return  0;
    }
    //发送不合适  并修改申请表的状态
    @PostMapping("/bossmanage/updateresumestatus1")
    public Integer updateResumeStatus1(Integer jobId,Integer userId){
        manageResumeService.sendImproper(jobId, userId);
        return  0;
    }
    //已经发送邀请的简历
    @GetMapping("/bossmanage/interviewedresume")
    public  ModelAndView interviewedResume(ModelAndView modelAndView,HttpServletRequest request){
        HttpSession session = request.getSession();
        CompanyUser companyUser = (CompanyUser) session.getAttribute("companyUser");
        //把已经发送邀请的简历个数查出来
        Integer resumeCount = manageResumeService.findInterviewedResumeNumber(companyUser);
        //查出字典表的学历，工作经历
        List<Dictionary>  edu = manageResumeService.findEduExperience1();
        List<Dictionary>  work = manageResumeService.findWorkExperience1();
        //查出发布的职位
        List<Job> jobs = manageResumeService.findJob1(companyUser);
        modelAndView.addObject("resumeCount",resumeCount);
        modelAndView.addObject("edu",edu);
        modelAndView.addObject("work",work);
        modelAndView.addObject("jobs",jobs);
        return modelAndView;
    }
    //已经邀请简历的信息
    @PostMapping("/bossmanage/interviewedresumeinfo")
    @ModelAttribute("pageInfo")
    public PageInfo<Apply> interviewedresumeinfo(Integer jobId,Integer workId,Integer eduId,Integer pageNum,Integer num,HttpServletRequest request){
        HttpSession session = request.getSession();
        CompanyUser companyUser = (CompanyUser) session.getAttribute("companyUser");
        //把已经邀请的简历全部查出来
        PageInfo<Apply> pageInfo = manageResumeService.findPendingResume(jobId,workId,eduId,pageNum,num,companyUser);
//
//        System.out.println(pageInfo.getList().get(0).getUser().getName());
        return pageInfo;

    }
    //不合适的简历页面
    @GetMapping("/bossmanage/improperresume")
    public  ModelAndView improperResume(ModelAndView modelAndView,HttpServletRequest request){
        HttpSession session = request.getSession();
        CompanyUser companyUser = (CompanyUser) session.getAttribute("companyUser");
//        //把不合适简历的个数查出来
        Integer resumeCount = manageResumeService.findPendingResumeNumber(companyUser,38);
        //查出字典表的学历，工作经历
        List<Dictionary>  edu = manageResumeService.findEduExperience1();
        List<Dictionary>  work = manageResumeService.findWorkExperience1();
        //查出发布的职位
        List<Job> jobs = manageResumeService.findJob1(companyUser);
        modelAndView.addObject("resumeCount",resumeCount);
        modelAndView.addObject("edu",edu);
        modelAndView.addObject("work",work);
        modelAndView.addObject("jobs",jobs);
        return modelAndView;


    }
    //不合适简历简历页面的显示 单独的一个页面
    @PostMapping("/bossmanage/improperresumeinfo")
    @ModelAttribute("pageInfo")
    public PageInfo<Apply> improperResumeInfo(Integer jobId,Integer workId,Integer eduId,Integer pageNum,Integer num,HttpServletRequest request){
        HttpSession session = request.getSession();
        CompanyUser companyUser = (CompanyUser) session.getAttribute("companyUser");

        //把不合适的简历全部查出来
        PageInfo<Apply> pageInfo = manageResumeService.findPendingResume(jobId,workId,eduId,pageNum,num,companyUser);
//
//        System.out.println(pageInfo.getList().get(0).getUser().getName());
        return pageInfo;

    }
    //未接受面试邀请的简历
    @GetMapping("/bossmanage/refuseinterview")
    public  ModelAndView refuseInterview(ModelAndView modelAndView,HttpServletRequest request){
        HttpSession session = request.getSession();
        CompanyUser companyUser = (CompanyUser) session.getAttribute("companyUser");
//        //把未接受邀请的简历的个数查出来
        Integer resumeCount = manageResumeService.findPendingResumeNumber(companyUser,37);
        //查出字典表的学历，工作经历
        List<Dictionary>  edu = manageResumeService.findEduExperience1();
        List<Dictionary>  work = manageResumeService.findWorkExperience1();
        //查出发布的职位
        List<Job> jobs = manageResumeService.findJob1(companyUser);
        modelAndView.addObject("resumeCount",resumeCount);
        modelAndView.addObject("edu",edu);
        modelAndView.addObject("work",work);
        modelAndView.addObject("jobs",jobs);
        return modelAndView;

    }
    //拒绝接受邀请简历简历页面的显示 单独的一个页面
    @PostMapping("/bossmanage/refuseinterviewinfo")
    @ModelAttribute("pageInfo")
    public PageInfo<Apply> refuseinterviewinfo(Integer jobId,Integer workId,Integer eduId,Integer pageNum,Integer num,HttpServletRequest request){
        HttpSession session = request.getSession();
        CompanyUser companyUser = (CompanyUser) session.getAttribute("companyUser");

        //把未接受邀请的简历全部查出来
        PageInfo<Apply> pageInfo = manageResumeService.findPendingResume(jobId,workId,eduId,pageNum,num,companyUser);
//
//        System.out.println(pageInfo.getList().get(0).getUser().getName());
        return pageInfo;

    }
    //已经接受面试邀请的简历页面
    @GetMapping("/bossmanage/acceptinterview")
    public  ModelAndView acceptinterview(ModelAndView modelAndView,HttpServletRequest request){
        HttpSession session = request.getSession();
        CompanyUser companyUser = (CompanyUser) session.getAttribute("companyUser");
//        //把已经接受接受邀请的简历的个数查出来
        Integer resumeCount = manageResumeService.findPendingResumeNumber(companyUser,36);
        //查出字典表的学历，工作经历
        List<Dictionary>  edu = manageResumeService.findEduExperience1();
        List<Dictionary>  work = manageResumeService.findWorkExperience1();
        //查出发布的职位
        List<Job> jobs = manageResumeService.findJob1(companyUser);
        modelAndView.addObject("resumeCount",resumeCount);
        modelAndView.addObject("edu",edu);
        modelAndView.addObject("work",work);
        modelAndView.addObject("jobs",jobs);
        return modelAndView;
    }
    //去查询已经接受邀请的简历  并且可以按时间段来查询简历，看看时间段
    @PostMapping("/bossmanage/acceptinterviewinfo")
    @ModelAttribute("pageInfo")
    public PageInfo<Apply> acceptinterviewinfo(String starttime,String endtime,Integer pageNum,Integer num,HttpServletRequest request){
        System.out.println("00222"+starttime);
        System.out.println("00222"+endtime);
        Date startTime=null;
        Date endTime=null;
        if(starttime==""){
            starttime="2010-10-10 11:11:11";
        }
        if(endtime==""){
            endtime = "2200-10-10 11:11:11";
        }
        //获得SimpleDateFormat类，我们转换为yyyy-MM-dd的时间格式
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            //使用SimpleDateFormat的parse()方法生成Date
            startTime = sf.parse(starttime);

            endTime= sf.parse(endtime);
            //打印Date
        } catch ( ParseException e) {
            e.printStackTrace();
        }
        HttpSession session = request.getSession();
        CompanyUser companyUser = (CompanyUser) session.getAttribute("companyUser");
        System.out.println("看看这个值"+startTime);
        System.out.println("看看这个值"+endTime);
        //把接受邀请的简历全部查出来  并且是按时间段来查询的
       PageInfo<Apply> pageInfo = manageResumeService.findAcceptInterviewResume(startTime,endTime,num,pageNum,companyUser);


        return pageInfo;

    }


}
